//
//  ViewController.m
//  手游
//
//  Created by tucici on 2018/9/16.
//  Copyright © 2018年 tucici. All rights reserved.
//
#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "ViewController.h"
#import <DD/DD.h>
@interface ViewController ()
{
//    NSString *_name;
    NSString *_isName;
    NSString *name;
    NSString *isName;

}
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) UIButton *pay;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancel:) name:DDPlatformCancelNotification object:nil];
    [self setValue:@"zwq" forKey:@"name"];
    
//    NSLog(@"_name:%@",_name);
    NSLog(@"_isName:%@",_isName);
    NSLog(@"name:%@",name);
    NSLog(@"isName:%@",isName);

    // Do any additional setup after loading the view, typically from a nib.
}
- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.backgroundColor = [UIColor purpleColor];
        _scrollView.bounces  = NO;
        _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
        [self.view addSubview:_scrollView];
        NSLayoutConstraint *attrTop = [NSLayoutConstraint constraintWithItem:_scrollView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        NSLayoutConstraint *attrLeft = [NSLayoutConstraint constraintWithItem:_scrollView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        NSLayoutConstraint *attrRight = [NSLayoutConstraint constraintWithItem:_scrollView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        NSLayoutConstraint *attrBottom = [NSLayoutConstraint constraintWithItem:_scrollView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        //                NSLayoutConstraint *attrBottom = [NSLayoutConstraint constraintWithItem:_scrollView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:SCREEN_HEIGHT];
        [self.view addConstraints:@[attrTop,attrLeft,attrRight,attrBottom]];
        
    }
    return _scrollView;
}
- (UIButton *)button
{
    if (!_button)
    {
        _button = [[UIButton alloc] init];
        _button.translatesAutoresizingMaskIntoConstraints = NO;
        [_button setTitle:@"登录" forState:UIControlStateNormal];
        [_button setBackgroundColor:[UIColor greenColor]];
        [_button addTarget:self action:@selector(actionForButton) forControlEvents:UIControlEventTouchUpInside];
        _button.layer.masksToBounds = YES;
        _button.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _button.layer.borderWidth = 0.5;
        [self.view addSubview:_button];
        NSLayoutConstraint *attrTop = [NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0];
        NSLayoutConstraint *attrBottom = [NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
        
        NSLayoutConstraint *attrWidth = [NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:100];
        NSLayoutConstraint *attrHeight = [NSLayoutConstraint constraintWithItem:_button attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:50];
        [self.view addConstraints:@[attrTop,attrWidth,attrHeight,attrBottom]];
    }
    return _button;
}

- (UIButton *)pay
{
    if (!_pay)
    {
        _pay = [[UIButton alloc] init];
        _pay.translatesAutoresizingMaskIntoConstraints = NO;
        
        [_pay setTitle:@"支付" forState:UIControlStateNormal];
        [_pay setBackgroundColor:[UIColor greenColor]];
        [_pay addTarget:self action:@selector(actionForPay) forControlEvents:UIControlEventTouchUpInside];
        _pay.layer.masksToBounds = YES;
        _pay.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _pay.layer.borderWidth = 0.5;
        [self.view addSubview:_pay];
        NSLayoutConstraint *attrTop = [NSLayoutConstraint constraintWithItem:_pay attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0];
        NSLayoutConstraint *attrLeft = [NSLayoutConstraint constraintWithItem:_pay attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:60];
        
        NSLayoutConstraint *attrWidth = [NSLayoutConstraint constraintWithItem:_pay attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeWidth multiplier:1.0 constant:100];
        NSLayoutConstraint *attrHeight = [NSLayoutConstraint constraintWithItem:_pay attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeHeight multiplier:1.0 constant:50];
        [self.view addConstraints:@[attrTop,attrWidth,attrHeight,attrLeft]];
    }
    return _pay;
}

- (void)actionForPay
{
    DDProductPara product = productManager().getProduct();
    product.goodsID = @"com.yjd.ydnz.6y——ss";//内购商品ID ---- 必传
    product.goodsTitle = @"测试商品标题";//内购商品名称 ---- 必传
    product.goodsPrice = @"0.01";//内购商品价格 ---- 必传
    product.serverID = @"0000";//游戏区服ID ---- 必传
    product.serverName = @"测试-天下第一区";//游戏区服名称 ---- 必传
    product.roleID = @"1002";//游戏角色ID ---- 必传
    product.roleName = @"测试角色名称";//游戏角色名称 ---- 必传
    product.roleLevel= @"0";
    product.notifyURL = @"http://101.200.125.112/api/gameserver/gamecallback.php";//游戏后台接收支付通知接口回调 ---- 必传
//    product.notifyURL =  @"https:apppay.rastargame.com/v2/Channel/payNotify/cchid/389";
    product.orderID = [self getCurrentDate]; //游戏订单号  ----- 非必传
    product.orderInfo = @"订单信息";//游戏订单信息  ----- 非必传
    platformManager().prepareZF(product);
//    [DDPlatform prepareZF:product];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SDK_Pay:) name:DDPlatformPayNotification object:nil];

}
//==========================================================================
- (NSString *)getCurrentDate
{
    //获取当前时间
    NSDate *now = [NSDate date];
    return [NSString stringWithFormat:@"%f",[now timeIntervalSince1970]];
}
- (void)actionForButton
{
//    [DDPlatform prepareLogin];
    platformManager().prepareLogin();
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SDK_Login:) name:DDPlatformLoginNotification object:nil];
    
}

- (void)SDK_Login:(NSNotification *)notifi
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:DDPlatformLoginNotification object:nil];
    
    NSLog(@"\nlogin success = %@\n",notifi.userInfo);
   NSString *accountid = [notifi.userInfo valueForKey:@"accountID"];
    NSString *token = [notifi.userInfo valueForKey:@"token"];

}
- (void)cancel:(NSNotification *)notifi
{
    NSLog(@"sdk返回/取消回调");
}
- (void)SDK_Pay:(NSNotification *)notifi
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:DDPlatformPayNotification object:nil];
    NSLog(@"\npay = %@\n",notifi.userInfo);

}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.button.hidden = NO;
    self.pay.hidden = NO;
    
    
}

- (BOOL)prefersStatusBarHidden
{
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:DDPlatformCancelNotification object:nil];
}

@end
